coreEquiptAcqConfig.editPage = {

    getGridParam : function () {
        var _this = this;
        return {
            type : 'grid',
            id : _this.idManager.limsEquiptAcqConfigEditListEquiptGrid,
            url : coreEquiptAcqConfig.equiptUrl + '/queries',
            page : true,
            columns : coreEquiptAcqConfig.getEditListColumns(),
            deleteFormatter : function (row) {
                return row.equiptCode;
            },
            onLoadSuccess : function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.cleanCompData([_this.idManager.limsEquiptAcqConfigEditListAcqConfigForm, _this.idManager.limsEquiptAcqConfigEditInstGrid]);
                }
            },
            onRowActive : function (index, row) {
                // 指令配置
                Gikam.getAsyncComp(_this.idManager.limsEquiptAcqConfigEditInstGrid).done(function (grid) {
                    grid.refresh({
                        url : Gikam.printf(coreEquiptAcqConfig.baseUrl + '/{id}/instructions/queries', {
                            id : row.id
                        })
                    });
                });
                // 采集设置
                Gikam.getAsyncComp(_this.idManager.limsEquiptAcqConfigEditListAcqConfigForm).done(function (form) {
                    form.refresh({
                        url : Gikam.printf(coreEquiptAcqConfig.baseUrl + '/{id}', {
                            id : row.id
                        })
                    });
                });

                // 分析项对照
/*                Gikam.getAsyncComp(_this.idManager.limsEquiptAcqConfigItemEditGrid).done(function (grid) {
                    grid.refresh({
                        url : Gikam.printf(coreEquiptAcqConfig.baseUrl + '/{id}/items/queries', {
                            id : row.id
                        })
                    });
                });*/
            },
            toolbar : [ {
                type : 'button',
                text : 'BASELINE.MODULE.COMMON.EQUIPTACQ.ACQCONFIGS.BUTTON.TEST_CONNECT',
                icon : 'enable',
                onClick : function () {
                    var equipt = Gikam.getComp(_this.idManager.limsEquiptAcqConfigEditListAcqConfigForm).getData();
                    if (equipt.acqCategory != 'serial' && equipt.acqCategory != 'net' && equipt.acqCategory != 'mqtt') {
                        Gikam.alert('BASELINE.MODULE.COMMON.EQUIPTACQ.ACQCONFIGS.TIP.ONLY_TCP_OR_SERIAL_SUPPORT');
                        return;
                    }
                    Gikam.create('modal', {
                        title : 'BASELINE.MODULE.COMMON.EQUIPTACQ.ACQCONFIGS.MODAL.TEST_CONNECT',
                        url : coreEquiptAcqConfig.baseUrl + '/page/connect-detail' + Gikam.param({
                            id : equipt.id
                        }),
                        onAfterClose : function () {

                        }
                    })
                }
            }]
        }
    },

    // 基本信息
    getBaseInfoTab : function () {
        var _this = this;
        return {
            type : 'tab',
            panels : [_this.getAcqConfigFormPanel()]
        }
    },

    getAcqConfigFormPanel : function () {
        var _this = this;
        return {
            title : 'BASELINE.MODULE.COMMON.RESOURCE.EQUIPT.ARCHIVESS.TAB.ACQ_CONFIG',
            items : [{
                type : 'form',
                id : _this.idManager.limsEquiptAcqConfigEditListAcqConfigForm,
                columns : 3,
                panels : [{
                    title : 'BASELINE.MODULE.COMMON.RESOURCE.EQUIPT.ARCHIVESS.TAB.ACQ_CONFIG_BASE_OPTION',
                    id : _this.idManager.limsEquiptAcqConfigEditListAcqConfigFormBasePanel,
                    fields : coreEquiptAcqConfig.getAcqConfigBaseFields()
                }, {
                    title : 'BASELINE.MODULE.COMMON.RESOURCE.EQUIPT.ARCHIVESS.TAB.ACQ_CONFIG_TCP_OPTION',
                    id : _this.idManager.limsEquiptAcqConfigEditListAcqConfigFormTcpPanel,
                    fields : coreEquiptAcqConfig.getAcqConfigTcpFields()
                }, {
                    title : 'BASELINE.MODULE.COMMON.RESOURCE.EQUIPT.ARCHIVESS.TAB.ACQ_CONFIG_MQTT_OPTION',
                    id : _this.idManager.limsEquiptAcqConfigEditListAcqConfigFormMqttPanel,
                    fields : coreEquiptAcqConfig.getAcqConfigMqttFields()
                }, {
                    title : 'BASELINE.MODULE.COMMON.RESOURCE.EQUIPT.ARCHIVESS.TAB.ACQ_CONFIG_SERIAL_OPTION',
                    id : _this.idManager.limsEquiptAcqConfigEditListAcqConfigFormSerialPanel,
                    fields : coreEquiptAcqConfig.getAcqConfigSerialFields()
                }, /*{
                    title : 'BASELINE.MODULE.COMMON.RESOURCE.EQUIPT.ARCHIVESS.TAB.ACQ_CONFIG_FILE_OPTION',
                    id : _this.idManager.limsEquiptAcqConfigEditListAcqConfigFormFilePanel,
                    fields : coreEquiptAcqConfig.getAcqConfigFileFields()
                }*/],
                service : 'coreEquiptAcqConfigService',
                onLoadSuccess : function (data) {
                    _this.showAcqConfigFields();
                },
                onUpdated : function (data) {
                    _this.showAcqConfigFields();
                }
            }]
        }
    },

    //显示隐藏
    showAcqConfigFields : function () {
        var _this = this;
        Gikam.getAsyncComp(_this.idManager.limsEquiptAcqConfigEditListAcqConfigForm).done(function (form) {
            var data = form.getData();
            if (data.acqCategory == 'net') {
                form.showPanelById([_this.idManager.limsEquiptAcqConfigEditListAcqConfigFormTcpPanel]);
                form.hidePanelById([_this.idManager.limsEquiptAcqConfigEditListAcqConfigFormSerialPanel,
                    _this.idManager.limsEquiptAcqConfigEditListAcqConfigFormMqttPanel]);
            } else if (data.acqCategory == 'serial') {
                form.showPanelById([_this.idManager.limsEquiptAcqConfigEditListAcqConfigFormSerialPanel]);
                form.hidePanelById([_this.idManager.limsEquiptAcqConfigEditListAcqConfigFormTcpPanel,
                    _this.idManager.limsEquiptAcqConfigEditListAcqConfigFormMqttPanel]);
            }else if (data.acqCategory == 'mqtt') {
                form.showPanelById([_this.idManager.limsEquiptAcqConfigEditListAcqConfigFormMqttPanel]);
                form.hidePanelById([_this.idManager.limsEquiptAcqConfigEditListAcqConfigFormSerialPanel,
                    _this.idManager.limsEquiptAcqConfigEditListAcqConfigFormTcpPanel]);
            }
            _this.showHideInstLayout(data.acqCategory);
        });
    },

    getInstGridParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : _this.idManager.limsEquiptAcqConfigEditInstGrid,
            columns : coreEquiptAcqConfig.getInstListColumns(),
            service : 'limsEquiptAcqConfigInstService',
            confirmField : 'instructionName',
            onCellClick : function(field, row) {
                field === 'script' && _this.editScript(row);
            },
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    var equiptGrid = Gikam.getComp(_this.idManager.limsEquiptAcqConfigEditListEquiptGrid);
                    var activedRow = equiptGrid.getActivedRow();
                    if (Gikam.isEmpty(activedRow)) {
                        return ;
                    }
                    Gikam.preInsert({
                        modalTitle : 'BASELINE.MODULE.COMMON.EQUIPTACQ.ACQCONFIGS.MODAL.ADD',
                        fields : coreEquiptAcqConfig.getInstInsertFormFields(),
                        url : Gikam.printf(coreEquiptAcqConfig.baseUrl + '/{id}/instructions', {
                            id : activedRow.id
                        }),
                        titleWidth : 100
                    }).done(function(id) {
                        Gikam.getComp(_this.idManager.limsEquiptAcqConfigEditInstGrid).refresh();
                    });
                }
            }, {
                   type : 'button',
                   text : 'T_LIMS_ACQ_CONFIG_INSTLIB.ADD.FROMINSTLIB',
                   icon : 'add',
                   onClick : function() {
                       var equiptGrid = Gikam.getComp(_this.idManager.limsEquiptAcqConfigEditListEquiptGrid);
                       var activedRow = equiptGrid.getActivedRow();
                       if (Gikam.isEmpty(activedRow)) {
                           return ;
                       }
                       Gikam.create('modal', {
                           title: Gikam.choose.getConfig('instlibChoose').title,
                           url: Gikam.choose.getConfig('instlibChoose').url + Gikam.param({
                           }),
                           onAfterClose: function (rows) {
                               if (Gikam.isEmpty(rows)) {
                                   return;
                               }
                               Gikam.getComp(_this.idManager.limsEquiptAcqConfigEditInstGrid).insert(Gikam.printf(coreEquiptAcqConfig.baseUrl + '/{id}/instructions', {
                                                                                                                         id : activedRow.id
                                                                                                                             }),rows);;
                           }
                       })
                   }
               },{
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    var equiptGrid = Gikam.getComp(_this.idManager.limsEquiptAcqConfigEditListEquiptGrid);
                    var activedRow = equiptGrid.getActivedRow();
                    if (Gikam.isEmpty(activedRow)) {
                        return ;
                    }
                    Gikam.getComp(_this.idManager.limsEquiptAcqConfigEditInstGrid).deleteRows(Gikam.printf(coreEquiptAcqConfig.baseUrl + '/{id}/instructions', {
                        id : activedRow.id
                    }));
                }
            } ]
        }
    },

    getEptItemGrid : function() {
        var _this = this;
        return {
            type : 'grid',
            id : _this.idManager.limsEquiptAcqConfigItemEditGrid,
            columns : coreEquiptAcqConfig.getEptItemListColumns(),
            service : 'limsEquiptAcqConfigItemService',
            confirmField : 'eptItemName',
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    var equipt = Gikam.getComp(_this.idManager.limsEquiptAcqConfigEditListEquiptGrid).getActivedRow();
                    if (Gikam.isEmpty(equipt)) {
                        return ;
                    }
                    Gikam.getComp(_this.idManager.limsEquiptAcqConfigItemEditGrid).insert(Gikam.printf(coreEquiptAcqConfig.baseUrl + '/{id}/items', {
                        id : equipt.id
                    }), [{}]);
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    var equipt = Gikam.getComp(_this.idManager.limsEquiptAcqConfigEditListEquiptGrid).getActivedRow();
                    if (Gikam.isEmpty(equipt)) {
                        return ;
                    }
                    Gikam.getComp(_this.idManager.limsEquiptAcqConfigItemEditGrid).deleteRows(Gikam.printf(coreEquiptAcqConfig.baseUrl + '/{id}/items', {
                        id : equipt.id
                    }));
                }
            } ]
        }
    },

    /**
     * 编辑解析脚本
     */
    editScript : function(row) {
        var _this = this;
        if (Gikam.isEmpty(row)) {
            return;
        }

        var simpleModal = Gikam.createSimpleModal({
            width: 520,
            height: 400,
            title : 'T_LIMS_EQUIPT_ACQ_CONFIG_INST.SCRIPT',
            showMaxBtn: false,
            bodyStyle: {
                padding: "0 16px"
            },
            toolbarFormatter: function () {
                return [{
                    type: "button",
                    text: "GIKAM.BUTTON.CONFIRM",
                    class: "blue",
                    onClick: function () {
                        Gikam.getComp(_this.idManager.limsEquiptAcqConfigEditInstGrid).setData({
                            index : row._index,
                            id : row.id,
                            script : form.getData().script
                        }, true);
                        Gikam.getLastModal().close();
                    }
                }, {
                    type: "button",
                    text: "GIKAM.BUTTON.CLOSE",
                    class: "red",
                    onClick: function () {
                        Gikam.getLastModal().close()
                    }
                }];
            }
        });

        var form = Gikam.create('form', {
            columns: 1,
            titleWidth: 0,
            fields : [{
                field : 'script',
                type : 'textarea',
                value : row.script,
                autoSave : false,
                height : 280,
                validators : ['strLength(0, 1200)'],
            }],
            renderTo: simpleModal.layout.options.center.$dom
        });
    },

    showHideInstLayout : function(acqCategory) {
        var _this = this;
        var acqLayout = Gikam.getComp(_this.idManager.limsEquiptAcqConfigEditListAcqConfigLayout);
        var grid = Gikam.getComp(_this.idManager.limsEquiptAcqConfigEditInstGrid);
        if (acqCategory == 'net' || acqCategory == 'serial' || acqCategory == 'mqtt') {
            acqLayout.setSize({
                south : {
                    height : 270
                }
            });
            if (grid != null){
                if (acqCategory == 'net' || acqCategory == 'serial'){
                    grid.hideColumns(["conversationRole","qos"]);
                    grid.showColumns(["hixFlag"]);
                    grid.refresh();
                }else if (acqCategory == 'mqtt'){
                    grid.hideColumns(["hixFlag"]);
                    grid.showColumns(["conversationRole","qos"]);
                    grid.refresh();
                }
            }
        } else {
            acqLayout.setSize({
                south : {
                    height : 0
                }
            })
        }
    },

    idManager : {
        limsEquiptAcqConfigEditListLayout : 'lims-equipt-acq-config-edit-list-layout',
        limsEquiptAcqConfigEditListAcqConfigLayout : 'lims-equipt-acq-config-edit-list-acq-layout',
        limsEquiptAcqConfigEditListEquiptGrid : 'lims-equipt-acq-config-edit-list-grid',
        limsEquiptAcqConfigEditListAcqConfigForm : 'lims-equipt-acq-config-edit-list-acq-config-form',
        limsEquiptAcqConfigEditListAcqConfigFormBasePanel : 'lims-equipt-acq-config-edit-list-acq-config-form-base-panel',
        limsEquiptAcqConfigEditListAcqConfigFormTcpPanel : 'lims-equipt-acq-config-edit-list-acq-config-form-tcp-panel',
        limsEquiptAcqConfigEditListAcqConfigFormSerialPanel : 'lims-equipt-acq-config-edit-list-acq-config-form-serial-panel',
        limsEquiptAcqConfigEditListAcqConfigFormMqttPanel : 'lims-equipt-acq-config-edit-list-acq-config-form-mqtt-panel',
        limsEquiptAcqConfigEditListAcqConfigFormFilePanel : 'lims-equipt-acq-config-edit-list-acq-config-form-file-panel',
        limsEquiptAcqConfigEditInstGrid : 'lims-equipt-acq-config-edit-list-inst-grid',
        limsEquiptAcqConfigItemEditGrid : 'lims-equipt-acq-config-item-edit-list-grid',
    },

    create : function () {
        var _this = this;
        Gikam.create('layout', {
            id : _this.idManager.limsEquiptAcqConfigEditListLayout,
            renderTo : workspace.window.$dom,
            west : {
                width : 600,
                items : [_this.getGridParam()]
            },
            center : {
                items : [{
                    type : 'layout',
                    id : _this.idManager.limsEquiptAcqConfigEditListAcqConfigLayout,
                    center : _this.getAcqConfigFormPanel(),
                    south : {
                        width : '0',
                        items : [{
                            type : 'layout',
                            center : {
                                width : '50%',
                                title : 'BASELINE.MODULE.COMMON.EQUIPTACQ.ACQCONFIGS.TAB.INSTRUCTION',
                                items : [_this.getInstGridParam()]
                            },
                      /*      center : {
                                title : 'BASELINE.MODULE.COMMON.EQUIPTACQ.ACQCONFIGS.TAB.INSTRUCTION',
                                items : [_this.getEptItemGrid()]
                            }*/
                        }]
                    }
                }]
            }
        });
    },

    init : function () {
        this.create();
    }
};
